const { getUserProfile, uploadBase64 } = require('../../../../api/index');
const { genderDists, getRandomString, toBase64 } = require('../../../../utils/util');

Component({
  properties: {
    modelShow: { 
      type: Boolean, 
      observer(newVal) {
        console.log('nnennn');
        this.setData({ modelShow: newVal })
      } 
    }
  },
  data: {
    text: '为了您更好的使用系统，请先根据学籍信息进行注册'
  },
  lifetimes: {
  },
  methods: {
    show() {
      this.setData({ modelShow: true });
    },
    hide() {
      this.setData({ modelShow: false });
    },
    async onChooseAvatar(event) {
      wx.showLoading({ title: '加载中...' });
      //  持久化 缓存头像
      fetchUploadAvatar.call(this, event.detail.avatarUrl);
      wx.hideLoading();
    },
    async onJump(event) {
      const userInfo = await getUserProfile();
      //  加工数据
      setResult.call(this, userInfo);
    },
  }
});

//  上传头像
async function fetchUploadAvatar(avatarUrl = '') {
  const _data = await toBase64(avatarUrl); //  将头像转成 base64
   const { errors, fid } = await uploadBase64(_data);
   if(errors.length > 0) { return; }
   wx.setStorageSync('avatarUrl', fid);
     //  选择完成头像在让其跳转
  wx.navigateTo({ url: '/packageA/pages/authentication/index/index' })
}

//  对数据进行加工处理
function setResult(data) {
  const _data = JSON.parse(JSON.stringify(data));
  _data.genderName = genderDists[ _data.gender ];
  wx.setStorageSync('baseUserInfo', _data);
  return _data;
}

